<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学习平台 - 54天编程学习计划</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            color: white;
            margin-bottom: 40px;
        }
        
        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .header p {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .card {
            background: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .login-form {
            max-width: 400px;
            margin: 0 auto;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 600;
            color: #333;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 40px;
        }
        
        .feature {
            text-align: center;
            padding: 20px;
        }
        
        .feature h3 {
            color: #333;
            margin-bottom: 10px;
        }
        
        .feature p {
            color: #666;
            line-height: 1.6;
        }
        
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 8px;
            display: none;
        }
        
        .alert.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .dashboard {
            display: none;
        }
        
        .user-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }
        
        .logout-btn {
            background: #dc3545;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .progress-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
        }
        
        .progress-card {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            border-left: 4px solid #667eea;
        }
        
        .stage-title {
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
        }
        
        .day-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            border-bottom: 1px solid #e9ecef;
        }
        
        .day-item:last-child {
            border-bottom: none;
        }
        
        .day-status {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: #e9ecef;
        }
        
        .day-status.completed {
            background: #28a745;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>54天编程学习平台</h1>
            <p>从Web基础到浏览器自动化的完整学习路径</p>
        </div>

        <!-- 登录界面 -->
        <div id="loginSection" class="card">
            <div class="login-form">
                <h2 style="text-align: center; margin-bottom: 30px; color: #333;">登录学习平台</h2>
                
                <div id="alertBox" class="alert"></div>
                
                <form id="loginForm">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" value="admin" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" value="admin123456" required>
                    </div>
                    
                    <button type="submit" class="btn">登录</button>
                </form>
                
                <div style="text-align: center; margin-top: 20px; color: #666;">
                    <small>默认账户：admin / admin123456</small>
                </div>
            </div>
        </div>

        <!-- 仪表板 -->
        <div id="dashboard" class="dashboard">
            <div class="card">
                <div class="user-info">
                    <h2>欢迎，<span id="userName"></span>！</h2>
                    <button id="logoutBtn" class="logout-btn">退出登录</button>
                </div>
                
                <div id="progressContainer" class="progress-grid">
                    <!-- 学习进度将通过JavaScript动态加载 -->
                </div>
            </div>
        </div>

        <!-- 功能介绍 -->
        <div class="features">
            <div class="card feature">
                <h3>🌐 Web基础</h3>
                <p>HTML5、CSS3、JavaScript核心技术，建立坚实的前端基础</p>
            </div>
            <div class="card feature">
                <h3>💻 C#编程</h3>
                <p>从基础语法到高级特性，掌握.NET生态系统开发</p>
            </div>
            <div class="card feature">
                <h3>⚡ Vue.js</h3>
                <p>现代前端框架，构建交互式Web应用程序</p>
            </div>
            <div class="card feature">
                <h3>🤖 CEF3实战</h3>
                <p>浏览器自动化技术，实现桌面应用与Web技术的完美结合</p>
            </div>
        </div>
    </div>

    <script>
        const API_BASE = '/api';
        let currentUser = null;
        let authToken = localStorage.getItem('token');

        // DOM元素
        const loginSection = document.getElementById('loginSection');
        const dashboard = document.getElementById('dashboard');
        const loginForm = document.getElementById('loginForm');
        const alertBox = document.getElementById('alertBox');
        const userName = document.getElementById('userName');
        const logoutBtn = document.getElementById('logoutBtn');
        const progressContainer = document.getElementById('progressContainer');

        // 显示提示信息
        function showAlert(message, type = 'error') {
            alertBox.textContent = message;
            alertBox.className = `alert ${type}`;
            alertBox.style.display = 'block';
            
            setTimeout(() => {
                alertBox.style.display = 'none';
            }, 5000);
        }

        // API请求函数
        async function apiRequest(url, options = {}) {
            const config = {
                headers: {
                    'Content-Type': 'application/json',
                    ...options.headers
                },
                ...options
            };

            if (authToken) {
                config.headers.Authorization = `Bearer ${authToken}`;
            }

            try {
                const response = await fetch(API_BASE + url, config);
                const data = await response.json();
                
                if (!response.ok) {
                    throw new Error(data.message || '请求失败');
                }
                
                return data;
            } catch (error) {
                console.error('API请求错误:', error);
                throw error;
            }
        }

        // 登录
        async function login(username, password) {
            try {
                const data = await apiRequest('/auth/login', {
                    method: 'POST',
                    body: JSON.stringify({ username, password })
                });

                authToken = data.token;
                currentUser = data.user;
                localStorage.setItem('token', authToken);
                
                showDashboard();
                showAlert('登录成功！', 'success');
                
            } catch (error) {
                showAlert(error.message);
            }
        }

        // 显示仪表板
        function showDashboard() {
            loginSection.style.display = 'none';
            dashboard.style.display = 'block';
            userName.textContent = currentUser.real_name || currentUser.username;
            loadProgress();
        }

        // 显示登录界面
        function showLogin() {
            loginSection.style.display = 'block';
            dashboard.style.display = 'none';
            currentUser = null;
            authToken = null;
            localStorage.removeItem('token');
        }

        // 加载学习进度
        async function loadProgress() {
            try {
                const data = await apiRequest('/progress');
                displayProgress(data.data);
            } catch (error) {
                showAlert('加载学习进度失败: ' + error.message);
            }
        }

        // 显示学习进度
        function displayProgress(progressData) {
            const stages = {
                1: { name: 'Web基础', color: '#667eea', days: [] },
                2: { name: 'C#编程', color: '#f093fb', days: [] },
                3: { name: 'Vue.js', color: '#4ecdc4', days: [] },
                4: { name: 'CEF3实战', color: '#45b7d1', days: [] }
            };

            // 分组进度数据
            progressData.forEach(item => {
                if (stages[item.stage]) {
                    stages[item.stage].days.push(item);
                }
            });

            // 生成HTML
            progressContainer.innerHTML = Object.entries(stages).map(([stageNum, stage]) => `
                <div class="progress-card">
                    <div class="stage-title" style="color: ${stage.color}">
                        第${stageNum}阶段: ${stage.name}
                    </div>
                    ${stage.days.map(day => `
                        <div class="day-item">
                            <span>第${day.day_number}天: ${day.title}</span>
                            <div class="day-status ${day.is_completed ? 'completed' : ''}"></div>
                        </div>
                    `).join('')}
                </div>
            `).join('');
        }

        // 事件监听
        loginForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            await login(username, password);
        });

        logoutBtn.addEventListener('click', showLogin);

        // 初始化
        if (authToken) {
            // 验证token是否有效
            apiRequest('/auth/profile')
                .then(data => {
                    currentUser = data.data;
                    showDashboard();
                })
                .catch(() => {
                    showLogin();
                });
        }
    </script>
</body>
</html> 